package com.lecture.java8.excel;

import com.alibaba.excel.EasyExcel;
import com.lecture.java8.excel.model.FinanceResult;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class FinanceMain {
    public static void main(String[] args) {
        /*String fileName = "D:\\workspace\\2023深能\\知识案例\\1\\data.xlsx";
        DemoDataListener<Finance> dataListener = new DemoDataListener<>();
        EasyExcel.read(fileName, Finance.class,dataListener).sheet().doRead();
        List<Finance> financeList = dataListener.getDataList();
        financeList = financeList.stream().filter(finance -> {
            return "2012".equals(finance.getYear());
        }).collect(Collectors.toList());

        System.out.println(financeList.size());*/

        String dataResultFileName = "D:\\study\\tmp\\0319\\3月19c.xlsx";
        DemoDataListener<FinanceResult> dataResultListener = new DemoDataListener<>();
        EasyExcel.read(dataResultFileName, FinanceResult.class,dataResultListener).sheet().doRead();
        List<FinanceResult> dataResultList = dataResultListener.getDataList();



        Map<String, List<FinanceResult>> map = dataResultList.stream()
                .collect(Collectors.groupingBy(FinanceResult::getStkcd));

        // 使用Stream API进行分组和筛选
        List<FinanceResult> filteredResults = map.entrySet().stream()
                .filter(entry -> hasAllYears(entry.getValue()))
                .flatMap(entry -> entry.getValue().stream())
                .collect(Collectors.toList());


        System.out.println(filteredResults.size());

        Map<String, List<FinanceResult>> map1 = filteredResults.stream().collect(Collectors.groupingBy(FinanceResult::getStkcd));
        System.out.println(map1.size());
        EasyExcel.write("D:\\study\\tmp\\0319\\不带st不带金融.xlsx", FinanceResult.class)
                .sheet("a").doWrite(filteredResults);
        /*Map<String, String> resultMap = new HashMap<>();
        dataResultList.stream().forEach(f -> {
            if (resultMap.containsKey(f.getCode() + "-" + f.getYear())) {
                System.out.println(f.getCode()+"-"+f.getYear()+"-"+f.getName());
            } else {
                resultMap.put(f.getCode() + "-" + f.getYear(),f.getJrzs());
            }

        });*/
        /*Map<String, String> resultMap = dataResultList.stream().collect(Collectors.toMap(r -> r.getCode() + "-" + r.getYear(), FinanceResult::getJrzs));


        String dataFileName = "D:\\study\\tmp\\0318\\数据3月18日.xlsx";
        DemoDataListener<Finance> dataListener = new DemoDataListener<>();
        EasyExcel.read(dataFileName, Finance.class,dataListener).sheet().doRead();
        List<Finance> financeList = dataListener.getDataList();
        for (Finance finance : financeList) {
            String kz = resultMap.get(finance.getB() + "-" + finance.getC());
            finance.setJrzs(kz);
        }

        EasyExcel.write("D:\\study\\tmp\\0318\\result.xlsx", Finance.class)
                .sheet("a").doWrite(financeList);*/


    }


    // 检查列表中是否包含2012到2022这十年的数据
    private static boolean hasAllYears(List<FinanceResult> resultList) {
        List<Integer> years = resultList.stream()
                .map(result -> {
                    String yearStr = result.getReptdt().substring(0, 4);
                    return Integer.parseInt(yearStr);
                })
                .distinct()
                .collect(Collectors.toList());

        int i = resultList.size()-1;
        for (int year = 2012; year <= 2022; year++) {
            FinanceResult financeResult = resultList.get(i);
            if (financeResult.getStknme().contains("ST") || financeResult.getStknme().contains("st") || financeResult.getNindnme().contains("金融")) {
                return false;
            }
            i--;
            if (i <0 ) {
                i=0;
            }
            if (!years.contains(year)) {
                System.out.println(resultList.get(0).getStknme());
                return false;
            }
        }
        return true;
    }
}
